<?php

namespace app\scm\controller;

use app\admin\common\Base;
use think\Db;
use app\scm\model\Invpu as Model;

class Invpupay extends Base
{
    public function index()
    {
        $data = $this->request->param();
        extract($data);

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (isset($data['startdate']) && isset($data['enddate'])) {
                // $sql = Db::name('order')->alias('a')->join('order_good b', 'a.id = b.order_id')->where('a.o_type', 4)->where('a.addtime', '>=', strtotime($data['startdate']))->where('a.addtime', '<=', strtotime($data['enddate']))->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId]);
                $sql = Db::name('order')->where('o_type', 4)->where('dj_time', '>=', strtotime($data['startdate']))->where('dj_time', '<=', strtotime($data['enddate']))->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId]);
                $sql = $data['supplierid'] == '' ? $sql : $sql->where('user_id', $data['supplierid']);
                // 高级搜索
                $sql = $sn == '' ? $sql : $sql->where('a.sn', $sn);

                if (!isset($supplier_category_id) || $supplier_category_id == 0 || $supplier_category_id == '') {
                } else {
                    $g = Db::name('customer')->where('category_id', $supplier_category_id)->select();
                    $g['ids'] = implode(',', array_column($g, 'id'));
                    $sql->where('user_id', 'in', $g['ids']); //商品类别
                }

                $list = $sql->select();
                $count = $sql->count();

                foreach ($list as $key => $value) {
                    $customer = Db::name('customer')->where('id', $value['user_id'])->find();
                    $list[$key]['supplier_category'] = Db::name('customer_category')->where('id', $customer['category_id'])->value('name');
                    $list[$key]['supplier_name'] = $customer['name'];
                    $list[$key]['type_name'] = '采购';
                    $list[$key]['addtime'] = date('Y-m-d', $value['dj_time']);
                    if ($payment_rate) {
                        $payRate = round($value['payment'] / $value['yh_price'] * 100, 2);
                        if ($payRate == 100) {
                            unset($list[$key]);
                        } else {
                            $list[$key]['payment_rate'] = $payRate . '%';
                        }
                    } else {
                        $list[$key]['payment_rate'] = round($value['payment'] / $value['yh_price'] * 100, 2) . '%';
                    }
                }

                $list0 = array_merge($list);
                $count = count($list0);
                $list = array_slice($list0, $start, $limit);

                return $this->grid_data($list, $count, $page, $limit);
            }
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('invpu')->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        // 启动事务
        Db::startTrans();
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $model = new Model();
        if (empty($data['id'])) {
            $data['addtime'] = time();
            $data['enterprise_id'] = $this->EnterpriseId;
            $result = $model->allowField(true)->save($data);
            $id = $model->id;
            if ($result) {
                $status = 1;
                $msg = "添加成功";
            }
        } else {
            $id = $data['id'];
            $result = $model->allowField(true)->save($data, ["id" => $id]);
            if ($result) {
                $status = 1;
                $msg = "更新成功";
            }
        }
        //子表
        self::saveDefautl($data, $id);
        Db::commit();

        return json(['status' => $status, 'msg' => $msg]);
    }

    private function saveDefautl($data, $pid)
    {
        if (isset($data['goods_id'])) {
            $invpuGoods = [];
            foreach ($data['goods_id'] as $keys => $v) {
                $temp = [
                    'invpu_id' => $pid,
                    'goods_id' => $v,
                    'goods_mark' => $data['goods_mark'][$keys],
                    'unit_id' => $data['unit'][$keys],
                    'store_id' => $data['store_id'][$keys],
                    'number' => $data['number'][$keys],
                    'base_unit_id' => $data['base_unit_id'][$keys],
                    'base_number' => $data['base_number'][$keys],
                    'buy_price' => $data['buy_price'][$keys],
                    'total_tax_price' => $data['total_tax_price'][$keys],
                    'discount_rate' => $data['discount_rate'][$keys],
                    'discount_price' => $data['discount_price'][$keys],
                    'price' => $data['price'][$keys],
                    'tax_rate' => $data['tax_rate'][$keys],
                    'tax_price' => $data['tax_price'][$keys],
                    'sum_price_tax' => $data['sum_price_tax'][$keys],
                    'purchase_price' => $data['purchase_price'][$keys],
                    'mark' => $data['mark'][$keys],
                    'purchase_sn' => $data['purchase_sn'][$keys],
                    'enterprise_id' => $this->EnterpriseId
                ];
                array_push($invpuGoods, $temp);
            }

            $rest = Db::name('invpu_goods')->insertAll($invpuGoods);
            if (!$rest) {
                // 回滚事务
                Db::rollback();
                return json(['status' => 0, 'msg' => '添加失败']);
            }
        } else {
            // 回滚事务
            Db::rollback();
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $del = Db::name('invpu')->where('id', $id)->update(["is_del" => 1]);
        if ($del) {
            $status = 1;
            $msg = "删除成功";
        }
        return json(['status' => $status, 'msg' => $msg]);
    }
}
